import scrapy
from scrapy_test.items import MovieItem

class DoubanSpider(scrapy.Spider):
    name = "douban"
    allowed_domains = ["movie.douban.com"]
    start_urls = ["https://movie.douban.com/chart"]

    def parse(self, response):
        #print(response.text)
        #titles = response.xpath('//tbody/tr/td[2]/div/a/text()').getall()
        #stars = response.xpath('//tbody/tr/td[2]/div/div/span[2]/text()').getall()
        titles = response.xpath('//div[@class="pl2"]/a/text()').getall()
        stars = response.xpath('//span[@class="rating_nums"]/text()').getall()
        titles = [title.strip()[:-1].strip() for title in titles if title.strip()]
        #print(f'titles.len: {len(titles)}, stars.len: {len(stars)}')

        for title , star in zip(titles, stars):
            print(f'title: {title}, star: {star}')
            yield{
                'title': title,
                'star': star,
            }
            item = MovieItem()
            item['title'] = title
            item['star'] = star
            yield item

